#!/usr/bin/python


def outlierCleaner(predictions, ages, 
                   net_worths, remove_count = 9):
    """
        Clean away the 10% of points that have the largest
        residual errors (difference between the prediction
        and the actual net worth).

        Return a list of tuples named cleaned_data where 
        each tuple is of the form (age, net_worth, error).
    """
    assert(len(predictions) == \
        len(ages) == len(net_worths))
    data = []
    for i in range(0,len(predictions)):
        error = net_worths[i] - predictions[i]
        elem  = (ages[i], net_worths[i], error)
        data.append(elem)
    sorted_list = sorted(
        data, key=lambda x: x[2]
    )
    cleaned_data = sorted_list[remove_count -1:-1]
    assert(len(cleaned_data) == len(ages) - remove_count)
    return cleaned_data

